package fr.u.psud.polytech.services;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;


public class UserGetter {
	public String getUsers()
	{
		String ret = "<users>\r\n";
		String url = "jdbc:mysql://localhost:3306/stock";
		
		try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(url, "root", "123loutre");
            Statement st = con.createStatement();
            ResultSet res = st.executeQuery("SELECT User.id," +
            		"User.nom, User.prenom, User.password, Entrepot.nom" +
            		" FROM User, Entrepot WHERE User.id_entrepot =Entrepot.id");
            
            while(res.next())
            {
            	String nom = res.getString("User.nom");
            	String prenom = res.getString("User.prenom");
            	//String poste = res.getString("User.poste");
            	String password = res.getString("User.password");
            	String entp = res.getString("Entrepot.nom");
            	
            	ret += "<user nom=\"" + nom +"\" prenom=\"" + prenom + "\" " //poste=\"" + poste +"\" 
            			+ " password=\"" + password + "\" entp=\"" + entp + "\" />\r\n";
            }
            ret += "</user>\r\n";
	    } catch (ClassNotFoundException e) {
	            e.printStackTrace();
	    } catch (SQLException e) {
			e.printStackTrace();
	    }
		
		return ret;
	}
	
	public int getrNbUser()
	{
		int ret =0;
		String url = "jdbc:mysql://localhost:3306/Stock";
		
		try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(url, "root", "123loutre");
            Statement st = con.createStatement();
            ResultSet res = st.executeQuery("SELECT User.id," +
            		"User.nom, User.prenom, User.password, Entrepot.nom" +
            		" FROM User, Entrepot WHERE User.id_entrepot =Entrepot.id");
            
            while(res.next())
            {
            	ret++;
            }

	    } catch (ClassNotFoundException e) {
	            e.printStackTrace();
	    } catch (SQLException e) {
			e.printStackTrace();
	    }
		
		return ret;
	}
	
	public String test()
	{
		return "helloworld";
	}
}
